﻿using System;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;

namespace MyMap
{
    public class MyMxdDocClass
    {
        private IMapDocument mapDocument;

        // 确保在需要时创建对象
        private IMapDocument MapDocument
        {
            get
            {
                if (mapDocument == null)
                {
                    mapDocument = new MapDocumentClass();
                }
                return mapDocument;
            }
        }

        // 新建地图文档
        public void newMapDoc(AxMapControl axMapControl)
        {
            try
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Title = "新建地图文档";
                saveFileDialog.Filter = "Mxd文档（*.mxd）|*.mxd";
                DialogResult dialogResult = saveFileDialog.ShowDialog();

                if (dialogResult != DialogResult.OK)
                {
                    return;
                }

                MapDocument.New(saveFileDialog.FileName);
                MapDocument.Open(saveFileDialog.FileName);
                axMapControl.Map = MapDocument.get_Map(0);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"新建文档失败: {ex.Message}");
            }
        }

        // 其他方法也要相应修改，使用 MapDocument 属性而不是 mapDocument 字段
        public void openMapDoc(AxMapControl axMapControl)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Title = "打开地图文档";
                openFileDialog.Filter = "Mxd文档（*.mxd）|*.mxd";
                DialogResult dialogResult = openFileDialog.ShowDialog();

                if (dialogResult != DialogResult.OK)
                {
                    return;
                }

                if (MapDocument.get_IsMapDocument(openFileDialog.FileName))
                {
                    MapDocument.Open(openFileDialog.FileName);
                    for (int i = 0; i < MapDocument.MapCount; i++)
                    {
                        axMapControl.Map = MapDocument.get_Map(i);
                    }
                    axMapControl.ActiveView.Refresh();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开文档失败: {ex.Message}");
            }
        }

        // 保存和另存为方法也要相应修改...
        public void saveMapDoc(AxMapControl axMapControl)
        {
            try
            {
                if (MapDocument.get_IsReadOnly(MapDocument.DocumentFilename))
                {
                    MessageBox.Show("地图文档只读！");
                    return;
                }

                MapDocument.Save();
                MessageBox.Show("保存成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存失败: {ex.Message}");
            }
        }

        public void SaveAsMapDoc(AxMapControl axMapControl)
        {
            try
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Title = "地图文档另存为";
                saveFileDialog.Filter = "Mxd文档（*.mxd）|*.mxd";
                DialogResult dialogResult = saveFileDialog.ShowDialog();

                if (dialogResult != DialogResult.OK)
                {
                    return;
                }

                if (MapDocument.get_IsReadOnly(MapDocument.DocumentFilename))
                {
                    MessageBox.Show("地图文档只读！");
                    return;
                }

                MapDocument.SaveAs(saveFileDialog.FileName);
                MapDocument.Open(saveFileDialog.FileName);
                MessageBox.Show("保存成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"另存为失败: {ex.Message}");
            }
        }

        // 添加释放资源的方法
        public void Dispose()
        {
            if (mapDocument != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(mapDocument);
                mapDocument = null;
            }
        }
    }
}